﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XemlUtilities.ISA
{
    public static class LinqExtensions
    {
        public static IEnumerable<System.Text.RegularExpressions.Match> Flaten(this IEnumerable<IEnumerable<System.Text.RegularExpressions.Match>> SetOfSetOfMatches)
        {
            foreach (IEnumerable<System.Text.RegularExpressions.Match> SetOfMatches in SetOfSetOfMatches)
            {
                foreach (System.Text.RegularExpressions.Match m in SetOfMatches)
                {
                    yield return m;
                }
            }
            yield break;
        }

        public static string StyleUpXemlFormat(this TimeSpan ts)
        {
            if (ts == new TimeSpan(0,0,0,0))
            {
                return "initiation";
            }
            else if (ts.Hours % 24 == 0 && ts.Minutes % 60 == 0 && ts.Seconds == 0)
            {
                return string.Format("{0:00}d", ts.Days);
            }
            else if (ts.Minutes % 60 == 0 && ts.Seconds == 0)
            {
                return string.Format("{0:00}d{1:00}h", ts.Days, ts.Hours);
            }
            else if (ts.Seconds == 0)
            {
                return string.Format("{0:00}d{1:00}h{2:00}m", ts.Days, ts.Hours, ts.Minutes);
            }
            else
            {
                return string.Format("{0:00}d{1:00}h{2:00}m{3:00}s", ts.Days, ts.Hours, ts.Minutes, ts.Seconds);
            }
        }

        public static string SafeGetString(this System.Data.SqlClient.SqlDataReader reader, int colIndex)
        {
            if(!reader.IsDBNull(colIndex))
                return reader.GetString(colIndex);
            else 
                return string.Empty;
        }
    }
}
